unit Testbed_01Scripter;

//  $URL: http://delphiscriptedtesting.googlecode.com/svn/InterposedVclScripter/Main/Testbed_01Scripter.pas $
//  $Revision: 1212 $
//  $Date: 2010-04-05 09:17:23 +0000 (Mon, 05 Apr 2010) $

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, AppEvnts, ComCtrls, StdCtrls, ExtCtrls;

type
  TTestScripter = class(TForm)
    apevScripter: TApplicationEvents;
    lblScripter: TLabel;
    stbScripter: TStatusBar;
    pnlHaltScriptButton: TPanel;
    btnHaltScript: TButton;
    tmrScripter: TTimer;
    btnPauseScript: TButton;
    btnCancelExit: TButton;
    procedure apevScripterActivate(Sender: TObject);
    procedure tmrScripterTimer(Sender: TObject);
    procedure btnHaltScriptClick(Sender: TObject);
    procedure btnPauseScriptClick(Sender: TObject);
    procedure btnCancelExitClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure ProgressReporter (Sender: TObject; const Message: string);
  end;

var
  TestScripter: TTestScripter;

implementation

{$R *.dfm}

uses
  VclScripter;


{ ================ TTestScripter ==================== }
{           Display progress of scripting             }
{ =================================================== }

procedure TTestScripter.apevScripterActivate(Sender: TObject);
begin
if Assigned (ScriptReader) then
  begin
  lblScripter.Caption :=
    Format('The application %s is executing under the control of script %s',
    [Application.ExeName, ScriptReader.ScriptFileName]);
  pnlHaltScriptButton.Visible := true;
  TestScripter.Visible := True;
  TestScripter.SetZOrder (False);
  ShowWindow(Application.MainFormHandle, SW_SHOWDEFAULT);
  ScriptReader.ProgressReporter := ProgressReporter;
  ScriptReader.Run(Sender);
  TestScripter.stbScripter.SimpleText :=
    Format('Script ended at line %d without exiting from the program.',
    [ScriptReader.ScriptLineNumber]);
  btnHaltScript.Enabled := false;
  btnPauseScript.Enabled := false;
  ShowWindow(Application.MainFormHandle, SW_SHOWDEFAULT);
  if ScriptReader.ScriptKill < maxint then
    tmrScripter.Enabled := true;
  end
else
if Assigned (ScriptWriter) then
  begin
  lblScripter.Caption :=
    Format('The application %s is recording GUI events as script %s',
    [Application.ExeName, ScriptWriter.ScriptFileName]);
  pnlHaltScriptButton.Visible := false;
  TestScripter.Visible := True;
  TestScripter.SetZOrder (False);
  ShowWindow(Application.MainFormHandle, SW_SHOWDEFAULT);
  end;
end;

procedure TTestScripter.btnCancelExitClick(Sender: TObject);
begin
tmrScripter.Enabled := false;
btnCancelExit.Enabled := false;
FreeAndNil(ScriptReader);
Close;
end;

procedure TTestScripter.btnHaltScriptClick(Sender: TObject);
begin
ScriptReader.Halt (Sender);
end;

procedure TTestScripter.btnPauseScriptClick(Sender: TObject);
begin
ScriptReader.Paused := not ScriptReader.Paused;
if ScriptReader.Paused then
  begin
  btnPauseScript.Caption := 'Resume Scri&pt';
  ScriptReader.Halt(Sender);
  end
else
  begin
  btnPauseScript.Caption := '&Pause Script';
  ScriptReader.Triggered := false;
  ScriptReader.Run(Sender);
  end;
end;

procedure TTestScripter.ProgressReporter(Sender: TObject;
  const Message: string);

// This is a very elementary implementation. It would be easy to think up something
// a little more sophisticated, for example, appending each message to a list box.

begin
stbScripter.SimpleText := Message;
end;

procedure TTestScripter.tmrScripterTimer(Sender: TObject);
begin
ScriptReader.ScriptKill := Int64(ScriptReader.ScriptKill) - Int64(tmrScripter.Interval);
if ScriptReader.ScriptKill <= 0 then
  PostQuitMessage(0);
lblScripter.Caption :=
  Format('The application %s was executing under the control of script %s, which did not close it. Forcing closed in %d seconds.',
    [Application.ExeName, ScriptReader.ScriptFileName, ScriptReader.ScriptKill div 1000]);
btnCancelExit.Enabled := true;
end;

end.
